package com.kuang.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.pojo.Books;
import com.kuang.pojo.User;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    //controller 调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍，并且展示全部的书籍

   @ResponseBody
   @RequestMapping(value = "/allBook",produces = "text/html;charset=utf-8")
    public String allBook(HttpServletRequest request,HttpServletResponse response){
        //查询书籍
       response.setContentType("text/html;charset=utf-8");
       /* 设置响应头允许ajax跨域访问 */
       response.setHeader("Access-Control-Allow-Origin", "*");
       /* 星号表示所有的异域请求都可以接受， */
       response.setHeader("Access-Control-Allow-Methods", "GET,POST");

       List<Books> list= bookService.queryAllBook();
       //字符串长度
       int count=list.size();
       //返回给前端
       return "{\"code\":200,\"message\":\"成功\",\"data\":" + JSONObject.toJSONString(list) + ",\"count\" :"+count+"}";
    }

    //小程序测试
    @GetMapping("/test")
    public String hello(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        /* 设置响应头允许ajax跨域访问 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /* 星号表示所有的异域请求都可以接受， */
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        return "你好呀";
    }
/*    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.setContentType("text/html;charset=utf-8");
        *//* 设置响应头允许ajax跨域访问 *//*
        response.setHeader("Access-Control-Allow-Origin", "*");
        *//* 星号表示所有的异域请求都可以接受， *//*
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        //获取微信小程序get的参数值并打印
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username="+username+" ,password="+password);
        List<Books> list= bookService.queryAllBook();
        //返回值给微信小程序
        Writer out = response.getWriter();
        out.write("进入后台了");
        out.flush();
    }*/
    //添加书籍
    @RequestMapping("/addBook")
    public  String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdatePage")
    public  String toUpdatePage(int id,Model model){
       Books books=bookService.queryBookById(id);
       model.addAttribute("QBooks",books);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除数据
    //Restfull风格删除
    @RequestMapping("/deleteBook")
    public String deleteBook( int id){
        System.out.print("id========"+id);
        bookService.deletBookById(id);
        return "redirect:/book/allBook";
    }

    //通过书名查询
    @ResponseBody
    @RequestMapping(value = "/queryBook",produces= "text/html;charset=utf-8")
    public  String queryBook(String queryBookName, Model model, HttpSession session){
       List<Books> books= bookService.queryBookByName(queryBookName);
      //获取字符串的长度
        int count=books.size();
        if (count<=0){
            model.addAttribute("err","未查到");
            return "";
        }else {
            return "{\"code\":200,\"message\":\"成功\",\"data\":" + JSONObject.toJSONString(books) + ",\"count\" :"+count+"}";
        }
    }
}
